<?php

namespace app\admin\controller\ykjp\inventory;

use app\common\controller\Backend;
use think\Db;

/**
 * 商品单位
 *
 * @icon fa fa-circle-o
 */
class Statement extends Backend {

    /**
     * Statement模型对象
     * @var \app\admin\model\ykjp\inventory\Statement
     */
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\ykjp\inventory\Statement;
        $this->view->assign("typeList", $this->model->getTypeList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index() {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            // if ($this->request->request('keyField')) {
            //     return $this->selectpage();
            // }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = Db::name('ykjp_inventory_statement')
                    ->alias('i')
                    ->join('ykjp_product p', 'i.product_id = p.id')
                    ->where($where)
                    ->where("i.firmid", $this->auth->firmid)
                    ->order('i.id', $order)
                    ->count();
            $list = Db::name('ykjp_inventory_statement')
                    ->alias('i')
                    ->join('ykjp_product p', 'i.product_id = p.id')
                    ->field('i.*,p.name,p.unit,p.prop,p.specification')
                    ->where($where)
                    ->where("i.firmid", $this->auth->firmid)
                    ->order('i.id', $order)
                    ->limit($offset, $limit)
                    ->select();
            $list = collection($list)->toArray();

            // 根据字段中储存的id，获取实际的值
            for ($i = 0; $i < count($list); $i++) {

                $w_id = $list[$i]['warehouse_id'];  //所在仓库id
                $p_id = $list[$i]['partition_id'];  //所在库区id

                $prop_list = json_decode($list[$i]['prop'], true);  //获取商品属性列表
                $prop = '';
                if (isset($prop_list)) {
                    // 解析商品属性
                    for ($j = 0; $j < count($prop_list); $j++) {
                        if (isset($prop_list[$j]['title'])) {
                            $title = $prop_list[$j]['title'];
                        } else {
                            $title = '';
                        }
                        if (isset($prop_list[$j]['value'])) {
                            $value = $prop_list[$j]['value'];
                        } else {
                            $value = '';
                        }
                        $prop .= $title . ':' . $value . ';';
                    }
                }
                $list[$i]['prop'] = $prop;  //商品属性
                //获取所在仓库名称
                $warehouse = Db::name('ykjp_warehouse')
                        ->field('name')
                        ->where("firmid", $this->auth->firmid)
                        ->where('id', $w_id)
                        ->find();
                $list[$i]['w_name'] = $warehouse['name'];

                //获取所在库区名称
                $partition = Db::name('ykjp_partition')
                        ->field('name')
                        ->where("firmid", $this->auth->firmid)
                        ->where('id', $p_id)
                        ->find();
                $list[$i]['p_name'] = $partition['name'];
            }
            //获取仓库
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

}
